//
//  KDDDVPNPage.m
//  KDExample
//
//  Created by YeQing on 2017/3/23.
//  Copyright © 2017年 naruto. All rights reserved.
//

#import "KDDDVPNPage.h"
#import "DiDiVPNKit.h"


@interface KDDDVPNPage () <DiDiVPNStoreDelegate>
@property (nonatomic, strong) DiDiVPNStore                  *vpnStore;
@property (nonatomic, strong)  UIButton                     *btnTest;
@end

@implementation KDDDVPNPage
#pragma mark - system
+(void)load
{
    [kNBCURLRouterInstance registerScheme:@"KDDDVPNPage" scheme:@"ddvpnpage"];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self.navigationItem setTitle:self.nbcRouterParams[@"name"]];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.btnTest];
}
-(void)viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear:animated];
    [self.vpnStore stopVPN];
}
- (void)dealloc
{
    BCLogDealloc();
    _vpnStore = nil;
}


#pragma mark - 事件
/**
 *  测试按钮点击事件
 *  @param sender:按钮
 *  @return void
 */
- (void)onBtnTestClick:(UIButton *)sender
{
    if(self.vpnStore.vpnStatus == DiDiVPNStatusConnected){
        [self.vpnStore stopVPN];
    }
    else{
        [self.vpnStore startVPN];
    }
    
}


#pragma mark - getter
-(DiDiVPNStore *)vpnStore
{
    if(!_vpnStore){
        _vpnStore = [DiDiVPNStore new];
        _vpnStore.delegate = self;
        _vpnStore.userName = @"username";
        _vpnStore.userPwd = @"pwd";
    }
    return _vpnStore;
}
-(UIButton *)btnTest
{
    if(!_btnTest){
        _btnTest = [[UIButton alloc] initWithFrame:CGRectMake(0, 100, self.view.frame.size.width, 50)];
        _btnTest.backgroundColor = [UIColor blueColor];
        [_btnTest setTitle:@"连接vpn" forState:UIControlStateNormal];
        [_btnTest setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btnTest setTitleColor:[UIColor redColor] forState:UIControlStateDisabled];
        [_btnTest addTarget:self action:@selector(onBtnTestClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _btnTest;
}

#pragma mark - DiDiVPNStoreDelegate
-(void)onDiDiVPNStore:(DiDiVPNStore *)vpnStore vpnStatusChange:(DiDiVPNStatus)vpnStatus
{
    switch (vpnStatus) {
        case DiDiVPNStatusInvalid:
        {
            [self.btnTest setTitle:@"未配置" forState:UIControlStateDisabled];
            self.btnTest.enabled = NO;
        }
            break;
        case DiDiVPNStatusDisconnected:
        {
            [self.btnTest setTitle:@"已断开,点击重连" forState:UIControlStateNormal];
            self.btnTest.enabled = YES;
        }
            break;
        case DiDiVPNStatusConnecting:
        {
            [self.btnTest setTitle:@"连接中..." forState:UIControlStateDisabled];
            self.btnTest.enabled = NO;
        }
            break;
        case DiDiVPNStatusConnected:
        {
            [self.btnTest setTitle:@"已连接,点击断开" forState:UIControlStateNormal];
            self.btnTest.enabled = YES;
        }
            break;
        case DiDiVPNStatusReasserting:
        {
            [self.btnTest setTitle:@"网络切换,正在重新..." forState:UIControlStateDisabled];
            self.btnTest.enabled = NO;
        }
            break;
        case DiDiVPNStatusDisconnecting:
        {
            [self.btnTest setTitle:@"正在断开..." forState:UIControlStateDisabled];
            self.btnTest.enabled = NO;
        }
            break;
        default:
            break;
    }
}
@end
